import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import login from '../views/login.vue'
import welcome from '../views/welcome.vue'
import users from '../components/users.vue'
import rote from '../components/rote.vue'
import product from '../components/product.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
  
    component: Home,
    redirect:'/home/welcome',
    children:[
      {path:'welcome',component:welcome},
      {path:'users',component:users},
      {path:'rote',component:rote},
      {path:'product',component:product}
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect:'/home/welcome',
    children:[
      {path:'welcome',component:welcome},
      {path:'users',component:users},
      {path:'rote',component:rote},
      {path:'product',component:product}
    ]
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  }
]

const router = new VueRouter({
  routes
})
//  后台管理系统不同于前台系统  必须登录才能看到 
router.beforeEach((to,from,next)=>{
  if(to.path === '/login') return next() 
  const tokenstr = window.sessionStorage.getItem('token')
  if(!tokenstr) {
     next('/login')
     return 
  }
  next()
})




export default router
